//
//  LGTeamDetailMemberView.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/2/16.
//  团队详情-成员

#import "LGTeamDetailMemberView.h"

#import "LGTeamDetailMemberCell.h"


#define lineHeight  viewPix(95)

@interface LGTeamDetailMemberView()<UICollectionViewDelegate,UICollectionViewDataSource>
/**   */
@property (nonatomic , strong)UIView *baseView;
/**   */
@property (nonatomic , strong)UICollectionView *collectionView;
/**   */
@property (nonatomic , strong)UIButton *actionBtn;
/**   */
@property (nonatomic , strong)NSArray *closeArray;


@end

@implementation LGTeamDetailMemberView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.baseView];
        [self.baseView addSubview:self.collectionView];
        [self.baseView addSubview:self.actionBtn];
        [self.baseView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.bottom.right.equalTo(self);
        }];
        [self.actionBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.bottom.right.equalTo(self);
            make.height.equalTo(@(viewPix(50)));
        }];
    }
    return self;
}

-(void)setCanEdit:(BOOL)canEdit{
    _canEdit = canEdit;
    [self.collectionView reloadData];
}

-(void)setDataArray:(NSArray *)dataArray{
    _dataArray = dataArray;
    NSInteger lineCount = isPad?16:10;
    if(dataArray.count>lineCount){
        self.closeArray = [dataArray subarrayWithRange:NSMakeRange(0, lineCount)];
    }else{
        self.closeArray = dataArray;
    }
    self.open = self.actionBtn.selected?YES:NO;
    [self.collectionView reloadData];
}

-(void)setOpen:(BOOL)open{
    _open = open;
    NSInteger lineCount = isPad?8:5;
    NSInteger count = (self.canEdit == YES) ? self.dataArray.count+2 : self.dataArray.count;
    NSInteger line = count/lineCount;
    NSInteger num = count%lineCount;
    line = (num>0) ? line+1:line;
    CGFloat bottomHeight = viewPix(50);
    if(line>2){
        //大于2行
        self.actionBtn.hidden = NO;
        bottomHeight = (self.canEdit == YES && num <= 2 && num>0 && open == YES)? bottomHeight-viewPix(30):bottomHeight;
        line = (open == NO)?2:line;
        
    }else{
        //小于2行
        self.actionBtn.hidden = YES;
        bottomHeight = (self.canEdit == YES && num <= 2 && num>0)?-viewPix(20):0;
    }
    
    self.collectionView.frame = CGRectMake(viewPix(8), viewPix(16), Screen_W-viewPix(48), lineHeight*line);
    if (self.delegate && [self.delegate respondsToSelector:@selector(memberViewHeight:)]) {
        [self.delegate memberViewHeight:(lineHeight*line+viewPix(16)+bottomHeight)];
    }
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if(self.canEdit == YES){
        if ((self.open == YES)) {
            return self.dataArray.count+2;
        }else{
            return self.closeArray.count+2;
        }
    }else{
        if ((self.open == YES)) {
            return self.dataArray.count;
        }else{
            return self.closeArray.count;
        }
    }
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    LGTeamDetailMemberCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    if(self.open == YES){
        if(indexPath.item<self.dataArray.count){
            cell.model = self.dataArray[indexPath.item];
        }else if(indexPath.item == self.dataArray.count){
            cell.model = nil;
            cell.iconName = @"团队详情-添加";
            
        }else if (indexPath.item == self.dataArray.count+1){
            cell.model = nil;
            cell.iconName = @"团队详情-删除";
        }
    }else{
        if(indexPath.item<self.closeArray.count){
            cell.model = self.closeArray[indexPath.item];
        }else if(indexPath.item == self.closeArray.count){
            cell.model = nil;
            cell.iconName = @"团队详情-添加";
            
        }else if (indexPath.item == self.closeArray.count+1){
            cell.model = nil;
            cell.iconName = @"团队详情-删除";
        }
    }
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    LGTeamDetailMemberCell *cell = (LGTeamDetailMemberCell *)[self.collectionView cellForItemAtIndexPath:indexPath];
    if(cell.model){
        if(self.delegate && [self.delegate respondsToSelector:@selector(checkMemberDetail:)]){
            [self.delegate checkMemberDetail:cell.model];
        }
    }else{
        NSString *type = [cell.iconName isEqualToString:@"团队详情-添加"]?@"add":@"delete";
        if(self.delegate && [self.delegate respondsToSelector:@selector(editMemberWithType:)]){
            [self.delegate editMemberWithType:type];
        }
    }
}

-(void)actionBtnTouched:(UIButton *)sender{
    sender.selected = !sender.selected;
    NSString *title = (sender.selected == YES)?@"收起":@"查看更多成员";
    [self.actionBtn setTitle:title forState:UIControlStateNormal];
    [self.actionBtn setButtonInsetsStyle:LGButtonInsetsStyleRight marginSpace:3];
    self.open = self.actionBtn.selected?YES:NO;
    [self.collectionView reloadData];
}


#pragma mark
#pragma mark ====> 创建控件
-(UIView *)baseView{
    if(!_baseView){
        _baseView = [[UIView alloc]init];
        _baseView.backgroundColor = [UIColor whiteColor];
        _baseView.cornerRidus = 15;
        _baseView.clipsToBounds = YES;
    }
    return _baseView;
}
-(UICollectionView *)collectionView{
    if(!_collectionView){
        NSInteger count = isPad?8:5;
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
        layout.itemSize = CGSizeMake((Screen_W-viewPix(48))/count, lineHeight);
        layout.minimumInteritemSpacing = 0;
        layout.minimumLineSpacing = 0;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(viewPix(8), viewPix(16), Screen_W-viewPix(48), lineHeight*2) collectionViewLayout:layout];
        [_collectionView registerClass:[LGTeamDetailMemberCell class] forCellWithReuseIdentifier:@"cell"];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
    }
    return _collectionView;
}

-(UIButton *)actionBtn{
    if(!_actionBtn){
        _actionBtn = [UIButton buttonWithTitle:@"查看更多成员" titleFont:LGFont(16) textColor:@"#666666" imageName:@"grayTipDown"];
        [_actionBtn setImage:[UIImage imageNamed:@"grayTipUp"] forState:UIControlStateSelected];
        _actionBtn.bounds = CGRectMake(0, 0, Screen_W-viewPix(32), viewPix(50));
        [_actionBtn setButtonInsetsStyle:LGButtonInsetsStyleRight marginSpace:3];
        [_actionBtn addTarget:self action:@selector(actionBtnTouched:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _actionBtn;
}



@end
